package xhwl.hmo.mall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.MesageAdapter;
import xhwl.hmo.mall.bean.OnItemClickListener;
import xhwl.hmo.mall.http.api.MessageListApi;
import xhwl.hmo.mall.http.bean.resp.HttpResp;
import xhwl.hmo.mall.http.bean.resp.MessageListBean;
import xhwl.hmo.mall.receiver.bean.PushType;
import xhwl.hmo.mall.utils.AppUtil;
import xhwl.hmo.mall.utils.StringUtil;
import xhwl.pulltorefresh.PullToRefreshRecyclerView;
import xhwl.pulltorefresh.callback.PullToRefreshListener;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;


public class MessageActivity extends BaseActivity implements PullToRefreshListener {

    private PullToRefreshRecyclerView mRecyclerView;
    MesageAdapter adapter;
    ArrayList<MessageListBean> mList = new ArrayList<>();
    private int mPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
    }

    @Override
    public void initViews() {
        initTitleViews();
        mRecyclerView = (PullToRefreshRecyclerView) findViewById(R.id.recyclerView);
    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText("系统消息");
    }

    @Override
    public void initListener() {
        mRecyclerView.setPullToRefreshListener(this);

    }

    @Override
    protected void processOper() {
        super.processOper();
        initRecylerView();
        adapter = new MesageAdapter(this, mList);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                MessageListBean bean = mList.get(position);
                String pushType=bean.getQUESITONID();
                if(StringUtil.isEmpty(pushType))
                    return;
                if(PushType.CERTIFICATE.equals(pushType)){
                    if(!AppUtil.isLogin()){
                        AppUtil.startLoginActivity(mContext);
                    }
                    else
                    {
                        Intent i = new Intent(mContext, PersonListActivity.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                        mContext.startActivity(i);
                    }
                }else if(PushType.CONSIGN.equals(pushType)){
                    if(!AppUtil.isLogin()){
                        AppUtil.startLoginActivity(mContext);
                    }
                    else
                    {
                        Intent i = new Intent(mContext, InfoOrderActivity.class);
                        i.putExtra(InfoOrderActivity.EXTRA_ORDER_ID,bean.getBUY_ID());
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
                        mContext.startActivity(i);
                    }
                }
            }
        });
    }

    @Override
    public void loadData() {
        getMessageList(1);
    }

    private void getMessageList(final int page_num) {
        MessageListApi api = new MessageListApi(page_num);
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<HttpResp<ArrayList<MessageListBean>>>() {
            @Override
            public void onNext(HttpResp<ArrayList<MessageListBean>> resp) {
                if (page_num == 1) {
                    if (resp != null) {
                        mRecyclerView.setRefreshComplete();
                        mPage = 1;
                        mList.clear();
                        if (resp != null && resp.data.size() > 0) {
                            mList.addAll(resp.data);
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        mRecyclerView.setRefreshFail();
                    }
                } else {
                    if (resp != null) {
                        mRecyclerView.setLoadMoreComplete();
                        if (resp != null && resp.data.size() > 0) {
                            mPage = page_num;
                            mList.addAll(resp.data);
                            adapter.notifyDataSetChanged();
                        } else {
                            mRecyclerView.setLoadMoreFail();
                        }
                    }
                }
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                if (page_num == 1) {
                    mRecyclerView.setRefreshFail();
                } else {
                    mRecyclerView.setLoadMoreFail();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        getMessageList(1);
    }

    @Override
    public void onLoadMore() {
        getMessageList(mPage + 1);
    }

    private void initRecylerView() {
        //设置EmptyView
        View emptyView = View.inflate(this, R.layout.layout_empty_view, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mRecyclerView.setEmptyView(emptyView);

        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView mRecyclerView, int dx, int dy) {
                super.onScrolled(mRecyclerView, dx, dy);


            }
        });
        //设置是否开启上拉加载
        mRecyclerView.setLoadingMoreEnabled(true);
        //设置是否开启下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        //设置是否显示上次刷新的时间
        mRecyclerView.displayLastRefreshTime(true);
    }
}
